using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace SS.core
{
	internal class manufacture
	{
		private Func objFunc = new Func();
		private string strConnection = "";
		public manufacture()
		{
			Program.strConnectionString = this.objFunc.strConnectionString;
		}
		public void loadToDataGridView(DataGridView dgv, BindingNavigator bdn)
		{
			dgv.AutoGenerateColumns = false;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string selectCommandText = "SELECT * FROM [TabManufacture]  WHERE [Man_ID] <> 'NSXMD' ORDER BY [Man_Name]";
			SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectCommandText, sqlConnection);
			DataSet dataSet = new DataSet();
			sqlDataAdapter.Fill(dataSet, "TabManufacture");
			BindingSource bindingSource = new BindingSource();
			bindingSource.DataSource = dataSet.Tables["TabManufacture"];
			bdn.BindingSource = bindingSource;
			dgv.DataSource = bindingSource;
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public void loadToListView(ListView lv, string searchFor)
		{
			lv.Items.Clear();
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT * FROM [TabManufacture] WHERE [Man_Name] LIKE N'%" + searchFor + "%' AND [Man_ID]<>'NSXMD' ORDER BY [Man_Name]";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			int num = 0;
			while (sqlDataReader.Read())
			{
				lv.Items.Add((num + 1).ToString());
				lv.Items[num].UseItemStyleForSubItems = false;
				lv.Items[num].SubItems.Add(sqlDataReader["Man_ID"].ToString());
				lv.Items[num].SubItems.Add(sqlDataReader["Man_Name"].ToString());
				num++;
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public void getInfo(string mID, TextBox txtID, TextBox txtName, TextBox txtAddress, TextBox txtPhone, TextBox txtFax, TextBox txtEmail, TextBox txtWebsite, TextBox txtGCustomer_Notes)
		{
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT * FROM [TabManufacture] WHERE [Man_ID] = '" + mID.ToString() + "' AND [Man_ID]<>'NSXMD'";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			while (sqlDataReader.Read())
			{
				txtID.Text = sqlDataReader["Man_ID"].ToString();
				txtName.Text = sqlDataReader["Man_Name"].ToString();
				txtAddress.Text = sqlDataReader["Man_Address"].ToString();
				txtPhone.Text = sqlDataReader["Man_Phone"].ToString();
				txtFax.Text = sqlDataReader["Man_Fax"].ToString();
				txtEmail.Text = sqlDataReader["Man_Email"].ToString();
				txtWebsite.Text = sqlDataReader["Man_Website"].ToString();
				txtGCustomer_Notes.Text = sqlDataReader["Man_Notes"].ToString();
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public int del(string id)
		{
			int result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand("DelManufacture", sqlConnection);
				sqlCommand.CommandType = CommandType.StoredProcedure;
				sqlCommand.Parameters.Add("ID", SqlDbType.VarChar);
				sqlCommand.Parameters["ID"].Value = id;
				sqlCommand.ExecuteNonQuery();
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = 0;
			}
			catch
			{
				result = 1;
			}
			return result;
		}
		public bool CheckInUse(string objectID)
		{
			bool result = false;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			try
			{
				SqlCommand sqlCommand = sqlConnection.CreateCommand();
				sqlCommand.CommandText = "SELECT TOP 1 PID FROM [TabProducts] WHERE [ManID] = '" + objectID + "'";
				SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
				if (sqlDataReader.HasRows)
				{
					result = true;
				}
				sqlDataReader.Close();
				sqlDataReader.Dispose();
			}
			catch
			{
			}
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
	}
}
